﻿using System;
using System.Linq;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.Context;

namespace Rio.Framework.Pages.CriteriaControls
{

    public partial class DateIndexControl 
    {

        /// <summary>
        /// Get or set the control value
        /// </summary>
        public override sealed string InputValue
        {
            get
            {
                DateTime ouputDate;
                string dateToParse = DateInput.MaskedText.Replace("_", "");
                if (DateTime.TryParse(dateToParse, out ouputDate))
                {
                    return ouputDate.ToShortDateString();
                }
                return string.Empty;
            }
            set
            {
                if (value != null)
                {
                    DateTime ouputDate;
                    if (DateTime.TryParse(value, out ouputDate))
                    {
                        DateInput.MaskedText = ouputDate.ToShortDateString();  // value;
                    }
                }
            }
        }

        //uncomment: 3.
        /// <summary>
        /// Gets or sets the input search value.
        /// </summary>
        /// <value>
        /// The input search value.
        /// </value>
        public override sealed string InputSearchValue
        {
            get
            {
                if (DateInput.Value != null && !String.IsNullOrEmpty(DateInput.Value.ToString()))
                {
                    String l_oStringTemp = String.IsNullOrWhiteSpace(DateInput.MaskedText) ? DateInput.Value as string : DateInput.MaskedText;
                    if (l_oStringTemp != null)
                    {
                        String[] l_oStringAsDate = l_oStringTemp.Split('/');

                        for (int i = 0; i < l_oStringAsDate.Count(); i++)
                        {
                            l_oStringAsDate[i] = l_oStringAsDate[i].Replace("_", "0");

                            int number;
                            bool result = Int32.TryParse(l_oStringAsDate[i], out number);
                            if (result && number == 0)
                            {
                                l_oStringAsDate[i] = "%";
                            }
                        }

                        l_oStringTemp = string.Format("{0}/{1}/{2}", l_oStringAsDate[0], l_oStringAsDate[1], l_oStringAsDate[2]);
                    }
                    return l_oStringTemp;
                }
                return String.Empty;
            }
            set
            {
                if (value != null)
                {
                    DateInput.Value = value;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DateIndexControl"/> class.
        /// </summary>
        public DateIndexControl()
        {
            InitializeComponent();
            _originalBorderBrush = this.DateInput.BorderBrush as SolidColorBrush ;

            string currentCulture = GlobalWebContext.RioAppSettings["Rio.Applications.Web.DefaultCulture"];
            switch (currentCulture)
            {
                case "fr-FR":
                    calendar.Culture = new CultureInfo("fr-FR");
                    DateInput.Mask = "##/##/####";
                    break;
                case "en-US":
                    calendar.Culture = new CultureInfo("en-US");
                    DateInput.Mask = "##/##/####";
                    break;
                case "zh-CN":
                    calendar.Culture = new CultureInfo("zh-CN");
                    DateInput.Mask = "####/##/##";
                    break;
            }
        }


        /// <summary>
        /// Create an instance of the control
        /// </summary>
        /// <param name="p_oindex"><see cref="Index"/> instance</param>
        /// <param name="P_szValue">Default value</param>
        /// <param name="P_SetDefaultValue">Use default rio value</param>
        public DateIndexControl(Index p_oindex, string P_szValue = null, bool P_SetDefaultValue = false)
            : this()
        {
            CurrentIndex = p_oindex;
            LabelControl.Text = CurrentIndex.IndexLabel;

            //if a value is selected at the start 
            if (P_szValue != null && !string.Empty.Equals(P_szValue))
            {
                InputValue = P_szValue;
            }
            else
            {
                if (P_SetDefaultValue)
                    this.SetDefaultValue();
            }

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;
            FilterButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Handles the SelectionChanged event of the calendar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void calendar_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            foreach (DateTime l_dt in e.AddedItems)
            {
                DateInput.Value = l_dt.ToShortDateString();
            }

            CalendarDropDownBtn.IsOpen = false;
        }

        /// <summary>
        /// Show filter window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonInput_Click(object sender, RoutedEventArgs e)
        {
            OnFilterClick(new Events.FilterClickEventArgs(this.CurrentIndex));
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// Clear input
        /// </summary>
        public override void Clear()
        {
            DateInput.MaskedText = string.Empty;
            DateInput.Value = null;
        }

        /// <summary>
        /// Throw validation event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DateInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueValidated(EventArgs.Empty);
        }

        /// <summary>
        /// Used to set the default index value
        /// </summary>
        public override sealed void SetDefaultValue()
        {
            if (!string.IsNullOrEmpty(this.CurrentIndex.DefaultValue))
            {
                if (this.CurrentIndex.DefaultValue.Equals("CURRENT_DATE")) //CURRENT_DATE is a special value to set the current datetime
                    InputValue = DateTime.Now.ToShortDateString();
                else
                    InputValue = this.CurrentIndex.DefaultValue;
            }
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.DateInput.BorderBrush = new SolidColorBrush(color);
        }

        /// <summary>
        /// Reverts the color of the border.
        /// </summary>
        public override void RevertBorderColor()
        {
            this.DateInput.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public override void SetMaxLength()
        {
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
            this.LabelControl.Text = string.Empty;
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
            this.FilterButton.Visibility = Visibility.Collapsed;
        }
    }
    
}
